package atlantik.peer;

import modelz.AbstractUpdateableCollectionListener;
import modelz.UpdateListener;
import atlantik.game.Atlantik;
import atlantik.game.Estate;
import atlantik.game.EstateGroup;
import atlantik.game.Player;
import atlantik.ui.PlayerView;

public class PlayerPeer extends AbstractPeer<Player, PlayerView> {

	public PlayerPeer(Atlantik client, Player item) {
		super(client, item, new PlayerView(client, item));
		client.getState().estateGroups().addListener(
				new AbstractUpdateableCollectionListener<EstateGroup>() {
					public void added(EstateGroup group) {
						updateSize();
					}

					public void removed(EstateGroup arg0) {
						updateSize();
					}
				});
		client.getState().estates().addListener(
				new AbstractUpdateableCollectionListener<Estate>() {
					public void added(Estate estate) {
						estate.addListener(new UpdateListener<Estate>() {
							public void updated(Estate arg0) {
								getComponent().repaint();
							}
						});
						updateSize();
					}

					public void removed(Estate estate) {
						updateSize();
					}
				});
	}

	private void updateSize() {
		getComponent().updateSize();
	}

	public void updated(Player player) {
		getComponent().updateSize();
		super.updated(player);
		getComponent().revalidate();
	}

}
